package demo3;
import java.util.HashMap;
import java.util.Map;

public class MapExample {
    public static void main(String[] args) {
        // 1. 创建 HashMap 并添加元素
        Map<String, Integer> map = new HashMap<>();
        map.put("Alice", 85);
        map.put("Bob", 90);
        map.put("Charlie", 78);

        // 2. 获取指定键的值
        int bobScore = map.get("Bob");
        System.out.println("Bob's score: " + bobScore);

        // 3. 遍历 HashMap
        System.out.println("\nAll students' scores:");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // 4. 删除指定键的元素
        map.remove("Charlie");
        System.out.println("\nMap after removing 'Charlie':");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}
